package com.privatework.backedserver.controller.login;

import com.google.common.collect.Maps;
import com.privatework.common.repository.user.UserRepository;
import com.privatework.common.requestvo.LoginReqVo;
import com.privatework.common.service.system.UserService;
import com.privatework.common.utils.ResultVoUtil;
import com.privatework.common.vo.ResultVo;
import com.privatework.common.vo.system.UserResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 登录 Controller
 *
 * @author johnny
 * @create 2020-07-10 下午4:14
 **/
@RestController
@Slf4j
@RequestMapping("/auth")
public class LoginController {


    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResultVo login(@RequestBody LoginReqVo loginReqVo) {
        log.info("【loginReqVo： {}】", loginReqVo);

        UserResultVo userResultVo = userService.findByUserName(loginReqVo.getUsername());
        if (userResultVo != null) {
            if (userResultVo.getPassword().equals(loginReqVo.getPassword())) {
                Map<String, String> resultMap = Maps.newHashMap();
                resultMap.put("token", userResultVo.getUsername() + "_" + userResultVo.getId());
                return ResultVoUtil.success(resultMap);
            }
        } else {
            return ResultVoUtil.loginFail();
        }
        return ResultVoUtil.loginFail();
    }

}